import {
  runAutoSC,runAutoGAM,runAutoTR,runAutoML,
} from '@/axios/api'
import { cloneDeep } from 'lodash'
import { roleDict } from '@/conf'
export default{
  data(){
    return {
      // 变量配置
      //  components:variableConfiguration
      // 验证方式
      verifMode:{type:1,training:80},
      //缺失值
      missing:{type:1,value:95,ismissing:1},
      //同质性
      homogeny:{type:1,value:95},
      //重要性评价
      importanceAssessment:{
        type:1,value:0.01,
        // IV:0,
        // ChiSquared:0,
        // Gini:0,
        // GBM:0,
        // Zvalue:0
      },
      //相关系数
      corr:{type:1,value:0.75},
      //分箱
      binn:{type:1,value:2,valueV:20},
      //模型
      regre:{
        type:0,
        restraintimes:100,error:0.001,
        max_depth:10,
        max_leaf_nodes:10,
        learning_rate:0.15,
        num_leaves:100
        },
      //交换参数
      exchangeParam:{scrate:20,scscore:600,scPDO:50},
      //模型评价
      evaluate:{
        KS:0,
        AUC:0,
        Gini:0,
        KSScore:0,
        KSpercentile:0,
        Rsquared:0,
        RMSE:0,
        NSSE:0,
      },
    }
  },
  methods:{
    async getResultAsync(module){
      const { modename } = this
      const POST_DATA = await this.postDate({module}).catch(e=>{
        throw new Error(e)
      })
      console.log({POST_DATA})
      if(modename === 'AutoSC')
        return await runAutoSC(POST_DATA)
      else if(modename === 'AutoGAM')
        return await runAutoGAM(POST_DATA)
      else if(modename === 'AutoTR')
        return await runAutoTR(POST_DATA)
      else if(modename === 'AutoML')
        return await runAutoML(POST_DATA)
    },
    async GET_CONF(){
      const { modename,nodeList } = this
      const confJSON = []
      const isJSON = true
      for(const node of nodeList){
        const { module } = node
        if(module === 0) continue 
        const data = await this.postDate({module,isJSON})
        confJSON.push(data)
      }
      return confJSON
    },
    async postDate({module,isJSON}){
      const { modelInfo,modeSample } = this
      const { id } = modelInfo
    	let mastInfo = { id,module }
    	let confJSON
      if(module===1){
    		const { variableConfiguration:{
          names_del,names_target,names_id
        },verifMode,modeSample:{id:sampleId} } = this
      //   const { targetRow } = modeSample
    		mastInfo.sampledId = modeSample.id
        if(names_id.length != 1) throw new Error('请规定1个ID')
        if(names_target.length != 1) throw new Error('请规定1个目标变量')
        confJSON = { names_del,names_target,names_id,verifMode,sampleId }
      }else if(module===2){
        const { missing,homogeny } = this
        confJSON = {missing,homogeny}
      }else if(module===3){
        const { importanceAssessment } = this
        confJSON =  {importanceAssessment}
      }else if(module===4){
        const { corr } = this
        confJSON =  {corr,}//聚类、共线性
      }else if(module===5){
        const { binn } = this
        confJSON =  {binn}
      }else if(module===6){
        const { regre } = this
        confJSON =  {regre}
      }else if(module===7){
        const { exchangeParam } = this
        confJSON =  {exchangeParam}
      }else if(module===8){
        confJSON =  {}
      }else {
        throw new Error("ERROR")
      }
      return isJSON?confJSON:Object.assign(mastInfo,{confJSON:JSON.stringify(confJSON)})
    },
  },
  computed:{
    allconf:{
      get:function(){
        const { verifMode,missing,homogeny,importanceAssessment,corr,
          regre,regre:{
            type,
            restraintimes,
            error,
            max_depth,
            max_leaf_nodes,
            learning_rate,
            num_leaves
          },
          evaluate,
          binn,exchangeParam
        } = this
        const { modeSample,modelInfo,modename,
          variableConfiguration:{names_del,names_target,names_id}
        } = this
        const regre_confs = modename==='AutoTR'? { type,max_depth,max_leaf_nodes,learning_rate,num_leaves }:{ restraintimes,error }
        const mode_confs = {modename,modelInfo,modeSample}
        const isRunEvaluate = [2,-1].includes(this.nodeList.find(e=>e.module===8).status)? {evaluate}:{}
        const isAutoSC = modename==='AutoSC'? {binn,regre:regre_confs,exchangeParam}:{regre:regre_confs}//为了顺序
        const other_modules_confs =  {
          missing,homogeny,//2
          importanceAssessment,//3
          corr,//4
        }
        return Object.assign(
          mode_confs,//title
          {names_del,names_target,names_id,verifMode},//module:1
          other_modules_confs,//module:2~4
          isAutoSC,//module:5~7
          isRunEvaluate//module:8
        )
      },
      set:function(data){
        if(Object.prototype.toString.call(data)==='[object Array]')
          data = Object.assign(...data)
        console.log(data)
        const {
          verifMode,names_id,names_del,names_target,
          missing,homogeny,
          importanceAssessment,
          corr,
          binn,
          regre,
          exchangeParam,
          evaluate
        } = data
        const haveid = names_id && names_id.length
        const havedel = names_del && names_del.length
        const havetarget = names_target && names_target.length
        for(let item of this.all_columnsData){
          const { name } = item
          if(haveid && names_id.includes(name)) item.role = 0
          if(havedel && names_del.includes(name)) item.del = 1
          if(havetarget && names_target.includes(name)) item.role = 2
        }
        this.verifMode = verifMode
        this.missing = missing
        this.homogeny = homogeny
        this.importanceAssessment = importanceAssessment
        this.corr = corr
        this.binn = binn
        this.regre = regre
        this.exchangeParam = exchangeParam
        this.evaluate = evaluate
      }
    },
    variableConfiguration(){
      const { all_columnsData } = this
      const names_id = []
      const names_del = []
      const names_target = []
      for(const {del,role,name} of all_columnsData){
        if(del) names_del.push(name)
        else{
          if(role===0) names_id.push(name)
          else if(role===2) names_target.push(name)
        }
      }
      return {names_del,names_target,names_id}
    }
  }
}